<?php

namespace app\api\controller;

use app\admin\model\Course;
use app\admin\model\shop\Config;
use app\api\model\mall\Order;
use app\api\model\mall\OrderGoods;
use app\api\model\shop\ShopDiscountOrder;
use app\api\model\UserConsumeLog;
use app\api\model\UserOverlordMealOrder;
use app\common\controller\Api;
use app\common\model\User;
use app\shopapi\model\ExtendConsumeShop;
use app\shopapi\model\lakala\ShopInfeed;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopBillsStat;
use app\shopapi\model\ShopUser;
use fast\Date;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //新手教程
    public function course()
    {
        $list = Course::order('weigh desc')->select();
        $this->success('',$list);
    }

    public function switch()
    {
        $switch = \app\common\model\Config::get(['name'=>'switch'])->value;
        $this->success('success',$switch);
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $ShopInfeed_model = new ShopInfeed();
        $pay_model = new \app\shopapi\model\lakala\Pay();

        $consume_log_info = UserConsumeLog::get(['order_no'=>'7271738339210035200']);
        $shop_id = 59;
        $user_id = 32;
        //UserConsumeLog::lockMemberConsumeCalculate($user_id,$consume_log_info->total_commission,$consume_log_info->consume_id,1);
        UserConsumeLog::newrecommendShopConsumeCalculate($user_id,$shop_id,$consume_log_info->total_commission,$consume_log_info->consume_id,0);
        UserConsumeLog::recommendShopConsumeCalculate($user_id,$shop_id,$consume_log_info->total_commission,$consume_log_info->consume_id,0);
        //UserConsumeLog::newrecommendShopConsumeCalculate(31,0,$consume_log_info->total_commission,$consume_log_info->consume_id,2);
        //$jd_model = new \app\api\model\cps\Jd();
        //$jd_model->unliquidated();
        dump(123);die;

        $this->success('请求成功');
    }


    public function task()
    {
        //确认收货+1天结算霸王餐
        /*$consume_list = UserConsumeLog::alias('ucl')
            ->join('user_overlord_meal_order uo','ucl.order_no = uo.order_id')
            ->where(['ucl.is_to_account'=>0,'ucl.is_refund'=>0,'ucl.cps_type'=>9])
            ->where('uo.complete_time', '<', time()-86400)
            ->field('ucl.consume_id,ucl.order_no')
            ->select();
        //dump($consume_list);die;
        foreach ($consume_list as $k=>$v){
            UserConsumeLog::sendCommission($v['order_no']);
            UserOverlordMealOrder::update(['status'=>2,'send_time'=>time()],['order_id'=>$v['order_no']]);
        }*/

        //UserConsumeLog::sendCommission('2801389161852521886');
        //dump(123);die;

        //自营订单未付款到期取消
        $order_list = Order::where(['orderstate'=>0,'paystate'=>0])
            ->where('expiretime','<',time())
            ->field('order_sn')->select();
        foreach ($order_list as $k=>$v){
            $orderInfo = \app\api\model\mall\Order::get(['order_sn'=>$v['order_sn']]);
            $orderInfo->save(['orderstate' => 2]);
            //库存恢复
            OrderGoods::setGoodsStocksInc($orderInfo->order_sn);
        }


        //京东cps订单
        //$page = input('page',1);
        $jd_model = new \app\api\model\cps\Jd();
        $jd_model->orderlist();

        //拼多多订单
        $pdd_model = new \app\api\model\cps\Pdd();
        $pdd_model->orderlist();


        //美团订单-团购
        $mt_model = new \app\api\model\cps\Mt();
        $mt_model->queryorder(2);

        //美团订单-外卖卡卷
        $mt_model->queryorder(1);


        //滴滴
        $dd_model = new \app\api\model\cps\Didi();
        $dd_model->order();


        //饿了么
        $elm_model = new \app\api\model\cps\Elm();
        $elm_model->order();


        //抖音
        $dy_model = new \app\api\model\cps\Dy();
        $dy_model->order();


        //淘宝客
        $tbk_model = new \app\api\model\cps\Tbk();
        $tbk_model->neworderlist();


        //唯品会
        $wph_model = new \app\api\model\cps\Wph();
        $wph_model->order();


        //转链
        $zl_model = new \app\api\model\cps\Common();
        $zl_model->order(0,'','',1);
        $zl_model->order(0,'','',2);

        $user_model = new User();

        $user_list = User::field('id')->select();
        foreach ($user_list as $k=>$v){
            $user_model->upgrade($v['id']);
        }


        $this->success();
    }

    public function task2()
    {
        //京东cps订单
        //$page = input('page',1);
        $jd_model = new \app\api\model\cps\Jd();
        $jd_model->unliquidated();

        //拼多多订单
        $pdd_model = new \app\api\model\cps\Pdd();
        $pdd_model->unliquidated();

        //美团订单-团购
        $mt_model = new \app\api\model\cps\Mt();
        //美团订单-外卖卡卷
        $mt_model->unliquidated();

        //饿了么
        $elm_model = new \app\api\model\cps\Elm();
        $elm_model->unliquidated();

        //抖音
        $dy_model = new \app\api\model\cps\Dy();
        $dy_model->unliquidated();

        //淘宝客
        $tbk_model = new \app\api\model\cps\Tbk();
        $tbk_model->unliquidated();

        //唯品会
        $wph_model = new \app\api\model\cps\Wph();
        $wph_model->unliquidated();

        //转链
        $zl_model = new \app\api\model\cps\Common();
        $zl_model->unliquidated();




        //拼多多根据收货时间十五天后发奖励
        $consume_list = UserConsumeLog::where(['is_to_account'=>0,'is_refund'=>0,'cps_type'=>1,'consume_type'=>2])
            ->where('receive_time','>',0)
            ->field('receive_time,order_no')
            ->select();
        foreach ($consume_list as $k=>$v){
            if ($v['receive_time']-15*86400>time()){
                UserConsumeLog::sendCommission($v['order_no']);
            }

        }
        $this->success();
    }

    //隔天发放奖励-折扣单
    public function send()
    {
        $starttime = Date::unixtime('day', -1);
        $endtime = Date::unixtime('day', -1, 'end');

        $consume_list = UserConsumeLog::where(['is_to_account'=>0,'is_refund'=>0,'cps_type'=>-1])
            ->where('pay_time', 'between time', [$starttime, $endtime])
            ->where('consume_type','in','0,1')
            ->field('consume_id,order_no')
            ->select();
        foreach ($consume_list as $k=>$v){
            UserConsumeLog::sendCommission($v['order_no']);
        }

        $this->success('success');
    }

    //每月15日发放上月的奖励-滴滴
    public function senddidi()
    {
        $starttime = strtotime(date('Y-m-01 00:00:00', strtotime("-1 month")));
        $endtime = strtotime(date('Y-m-t 23:59:59', strtotime("-1 month")));

        $consume_list = UserConsumeLog::where(['cps_type'=>5,'consume_type'=>2,'is_to_account'=>0,'is_refund'=>0])
            ->where('pay_time', 'between time', [$starttime, $endtime])
            ->field('consume_id,order_no')
            ->select();

        foreach ($consume_list as $k=>$v){
            UserConsumeLog::sendCommission($v['order_no']);
        }

        $this->success('success',$consume_list);
    }


    //配置
    public function config()
    {
        $config = \app\admin\model\Config::get(1);

        $this->success('',$config);
    }
}
